//
//  PhotoContainerView.m
//  Version1.0_RectiveCocoa
//
//  Created by 吴可高 on 2018/1/20.
//  Copyright © 2018年 吴可高. All rights reserved.
//

#import "PhotoContainerView.h"
#import "PhotoCell.h"
static NSString * cellDefiner = @"PhotoCell";
@interface PhotoContainerView()<UITableViewDelegate,UITableViewDataSource>
@end
@implementation PhotoContainerView
#pragma mark - getter methods

-(UITableView *)tableView{
    _tableView = ({
        if (!_tableView) {
            _tableView  = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
            _tableView.delegate = self;
            _tableView.dataSource = self;
            _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
            [_tableView registerClass:[PhotoCell class] forCellReuseIdentifier:cellDefiner];
        }
        _tableView;
    });
    return _tableView;
}
-(void)setAlbums:(NSMutableArray *)albums{
    _albums = albums;
    if (_albums) {
        [self.tableView reloadData];
    }
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
-(void)setup{
    __weak  typeof(self)weakSelf = self;
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(weakSelf).with.insets(UIEdgeInsetsZero);
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.albums.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCell * cell = [tableView dequeueReusableCellWithIdentifier:cellDefiner forIndexPath:indexPath];
    cell.model = self.albums[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AlbumModel * model = self.albums[indexPath.row];
    if (self.completeBlock) {
        self.completeBlock(indexPath,model);
    }
}
@end
